﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fortuitus.Core
{
    public static class VectorStatisticExtensions
    {
        /// <summary>
        /// Checks whether an Vector is empty or just contains missing values (NaN).
        /// </summary>
        /// <param name="vector">The input vector of numbers.</param>
        /// <returns>An value indicating whether the Vector is empty or consists of missing values only.</returns>
        public static bool IsEmptyOrHasNoValues(this IVector vector)
        {
            bool hasValues = false;
            foreach (double number in vector)
            {
                if (!double.IsNaN(number))
                {
                    hasValues = true;
                }
            }
            return hasValues;
        }

        /// <summary>
        /// Calculates the arithmetic mean of an vector of numbers and ignores missing values (NaN).
        /// </summary>
        /// <param name="vector">The input vector of numbers.</param>
        /// <returns>The arithmetic mean of the numbers from the input vector.</returns>
        public static double Mean(this IVector vector)
        {
            double sum = 0;
            int count = 0;
            foreach (double number in vector)
            {
                if (!double.IsNaN(number))
                {
                    count++;
                    sum += number;
                }
            }
            if (count == 0)
                throw new ArgumentException("The input vector doesn't contain any non-MissingValues.", "vector");

            double mean = sum / count;
            return sum / count;
        }

    }
}
